Python Fastlane Crash Course: The Smart Way To Learn Python Proramming by Goldmeyr Martin

Python Fastlane Crash Course: The Smart Way To Learn Python Proramming by Goldmeyr Martin

Author:Goldmeyr, Martin [Goldmeyr, Martin]
Language: eng
Format: epub
Published: 2020-03-30T16:00:00+00:00


That is how we can have a basic Python function.

Function Parameters

You can dynamically define arguments for a function. Example:

#!/usr/bin/python3

def getSum(p, q):

result = p + q

print('The first number is', p)

print('The second number is', q)

print("The sum is", result)

getSum(19,10)

The code returns the following result:

We defined a function named getSum . The function takes two parameters, namely p and q . We have another variable named result, which is the sum of the two function parameters. In the last statement, we have called the function and passed the values for the two parameters. The function will calculate the value of the variable result by adding the two numbers. We finally get the result shown above.

Note that during our function definition, we specified two parameters, p and q. Try to call the function will either more than two parameters or 1 parameter and see what happens. Example:

#!/usr/bin/python3

def getSum(p, q):

result = p + q

print('The first number is', p)

print('The second number is', q)

print("The sum is", result)

getSum(19)

In the last statement in our code above, we have passed only one argument to the function, that is, 19. The program gives an error when executed:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.